//
//  DesignPreferencesVC.m
//  BigJoy
//
//  Created by Rowling on 2017/7/4.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "DesignPreferencesVC.h"
#import "DesignPreferenceCell.h"
@interface DesignPreferencesVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *topContentView;
@property (weak, nonatomic) IBOutlet UIView *bottmoView;
@property (nonatomic,strong) UICollectionView *topCollectionView;
@property (nonatomic,strong) UICollectionView *bottmoCollcetionView;

@end

@implementation DesignPreferencesVC

-(UICollectionView *)topCollectionView {
    if (_topCollectionView == nil) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(75,30);
        layout.minimumLineSpacing  = 10;
        layout.sectionInset = UIEdgeInsetsMake(0, 20,0,20);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _topCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, XMH(50), SCREEN_WIDTH, 0.6 * SCREEN_HEIGHT + XMH(20)) collectionViewLayout:layout];
        [_topCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([DesignPreferenceCell class]) bundle:nil] forCellWithReuseIdentifier:@"TopCell"];
        _topCollectionView.backgroundColor = [UIColor clearColor];
        _topCollectionView.showsHorizontalScrollIndicator = NO;
        _topCollectionView.delegate = self;
        _topCollectionView.dataSource = self;
    }
    return _topCollectionView;
}

-(UICollectionView *)bottmoCollcetionView {
    if (_bottmoCollcetionView == nil) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(75,30);
        layout.minimumLineSpacing  = 10;
        layout.sectionInset = UIEdgeInsetsMake(15,20,0,20);
        _bottmoCollcetionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, XMH(50), SCREEN_WIDTH, 0.6 * SCREEN_HEIGHT + XMH(20)) collectionViewLayout:layout];
        [_bottmoCollcetionView registerNib:[UINib nibWithNibName:NSStringFromClass([DesignPreferenceCell class]) bundle:nil] forCellWithReuseIdentifier:@"BottomCell"];
        _bottmoCollcetionView.backgroundColor = [UIColor clearColor];
        _bottmoCollcetionView.showsHorizontalScrollIndicator = NO;
        _bottmoCollcetionView.delegate = self;
        _bottmoCollcetionView.dataSource = self;
    }
    return _bottmoCollcetionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.topContentView addSubview:self.topCollectionView];
    [self.topCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.top.equalTo(self.topContentView);
    }];
    [self.bottmoView addSubview:self.bottmoCollcetionView];
    [self.bottmoCollcetionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.top.equalTo(self.bottmoView);
    }];
    self.baseDataArray = @[@"金",@"木",@"水",@"火",@"土",@"蛇",@"神",@"鬼",@"牛"];
    [self.baseMutableDataArray addObjectsFromArray:@[@"牛"]];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.topCollectionView == collectionView) {
        return self.baseMutableDataArray.count;
    }
    return self.baseDataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.topCollectionView == collectionView) {
        DesignPreferenceCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TopCell" forIndexPath:indexPath];
        [cell.button setTitle:self.baseMutableDataArray[indexPath.row] forState:UIControlStateNormal];
        return cell;
    }else {
        DesignPreferenceCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BottomCell" forIndexPath:indexPath];
        [cell.button setTitle:self.baseDataArray[indexPath.row] forState:UIControlStateNormal];
        [cell.button setTitleColor:XMColor(245, 70, 70) forState:UIControlStateNormal];
        [cell.button setBackgroundColor:[UIColor whiteColor]];
        for (NSString * str in self.baseMutableDataArray) {
            if ([str isEqualToString:self.baseDataArray[indexPath.row]]) {
                [cell.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [cell.button setBackgroundColor:XMColor(245, 70, 70)];
            }
        }
        return cell;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
     if (self.topCollectionView == collectionView) {
         [self.baseMutableDataArray removeObjectAtIndex:indexPath.row];
     }else {
         int temp = -1;
         for (int i = 0;i < self.baseMutableDataArray.count; i ++) {
             if ([self.baseMutableDataArray[i] isEqualToString:self.baseDataArray[indexPath.row]]) {
                 temp = i;
             }
         }
         if (temp < 0) {
             [self.baseMutableDataArray addObject:self.baseDataArray[indexPath.row]];
         } else {
             [self.baseMutableDataArray removeObjectAtIndex:temp];
         }
    }
    [self.topCollectionView reloadData];
    [self.bottmoCollcetionView reloadData];
}


- (IBAction)completeButtonClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)cancelButtonClick:(id)sender {
     [self dismissViewControllerAnimated:YES completion:nil];
}

@end
